package utils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Stack;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XMLSerializer implements Serializer{

	private Stack<Object> stack = new Stack<Object>();
	private File file;
	
	public XMLSerializer(File file){
	    this.file = file;
	}
	
	@Override
	public void push(Object o) {
		// TODO Auto-generated method stub
		stack.push(o);
	}

	@Override
	public Object pop() {
		// TODO Auto-generated method stub
		return stack.pop();
	}

	@Override
	public void write() throws Exception {
		// TODO Auto-generated method stub
		ObjectOutputStream os = null;
	    try
	    {
	      XStream xstream = new XStream(new DomDriver());
	      os = xstream.createObjectOutputStream(new FileWriter(file));
	      while (!stack.empty())
	      {
	        os.writeObject(stack.pop());  
	      }
	    }
	    finally
	    {
	      if (os != null)
	      {
	        os.close();
	      }
	    }
	}

	@Override
	public void read() throws Exception {
		// TODO Auto-generated method stub
		ObjectInputStream is = null;
	    try
	    {
	      XStream xstream = new XStream(new DomDriver());
	      is = xstream.createObjectInputStream(new FileReader(file));
	      Object obj = is.readObject();
	      while (obj != null)
	      {
	        stack.push(obj);
	        obj = is.readObject();
	      }
	    }catch(Exception e){
	    	is.close();
	    }
		
		
//		  ObjectInputStream is = null;
//		  try{
//			  FileWriter file = new FileWriter("datastore.json");
//			  XStream xstream = new XStream(new DomDriver());
//			  InputStream io = new FileInputStream(new File("datastore.xml"));
//		
			  
//		      is = xstream.createObjectInputStream(new FileReader(new File("datastore.xml")));
//		      Object obj = is.readObject();
//		      while (obj != null)
//		      {
//		        obj = is.readObject();
//		        XStream xstream1 = new XStream(new JettisonMappedXmlDriver());
//		        xstream1.setMode(XStream.NO_REFERENCES);
//		        String jSON = xstream1.toXML(obj);
//		        file.write(new org.codehaus.jettison.json.JSONObject(jSON).toString());
//		        file.flush();
//		      }
//		      file.close();
//		      
//		  }
//		  finally
//		  {
//			  
//		  }
	}
	
	// Change format file
	@Override
	public void changeFormatFile(String sourceFileExt, String targetFileExt) throws Exception{
		// Read data from XML file and put it into stack
		read();
		
		FileWriter file = new FileWriter("datastore.json");
		
		// After that based on target file extendsion to convert.
		while (!stack.empty())
	      {
			XStream xstream1 = new XStream(new JettisonMappedXmlDriver());
	        xstream1.setMode(XStream.NO_REFERENCES);
	        String jSON = xstream1.toXML(stack.pop());
	        file.write(new org.codehaus.jettison.json.JSONObject(jSON).toString());
	        System.out.println(jSON);
	        file.flush(); 
	      }
		file.close();
	}
}
